<?php
/**
 *   Joomdiet - administrator/components/models/food.php
 *   Author: Juan Dapena Paz (juan@bittingbits.com)
 *   Copyright (C) 2010 Bitting Bits S.L.
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

defined('_JEXEC') or die('Restricted access');

jimport( 'joomla.application.component.controller' );
class JoomdietControllerFood extends JController
{
    function __construct() {
        // get requested view
        if(JRequest::getCmd('view') == '') {
            JRequest::setVar('view', 'default');
        }
        $this->item_type = 'Default';
        parent::__construct();
        
        $this->registerTask('addFood', 'editFood');
    }
    
    function deleteFood()
    {
        $model = $this->getModel('food');
        $cid = JRequest::getVar( 'cid', array(0), 'post', 'array' );
        
        if (!is_array($cid) || count($cid) < 1) {
            $msg = '';
			JError::raiseWarning(500, JText::_( 'COM_JOOMDIET_SELECT_DELETE' ) );
        } else {
            foreach ($cid as $id) {
                if (!$model->delete($id)) {
                    JError::raiseError(500, JText::_('COM_JOOMDIET_DELETE_FAILED').'<pre>'.print_r($cid, true).'</pre>');
                }
            }
            
            $msg = count($cid).' '.JText::_( 'COM_JOOMDIET_SELECTED_DELETE' );
			$cache = &JFactory::getCache('com_joomdiet');
			$cache->clean();
        }
        
        $this->setRedirect( 'index.php?option=com_joomdiet&view=food', $msg );
    }
    
    function directDeleteFood()
    {
        $model = $this->getModel('food');
        $cid = JRequest::getVar( 'cid', array(0), 'request', 'array' );
        
        if (!is_array($cid) || count($cid) < 1) {
            $msg = '';
			JError::raiseWarning(500, JText::_( 'COM_JOOMDIET_SELECT_DELETE' ) );
        } else {
            foreach ($cid as $id) {
                if (!$model->delete($id)) {
                    $this->setRedirect('index.php?option=com_joomdiet&view=food',
                                       JText::_('COM_JOOMDIET_FOOD_DELETE_FAILED'),
                                       'error');
                    return;
                }
            }
            
            $msg = count($cid).' '.JText::_( 'COM_JOOMDIET_SELECTED_DELETE' );
			$cache = &JFactory::getCache('com_joomdiet');
			$cache->clean();
        }
        
        $this->setRedirect( 'index.php?option=com_joomdiet&view=food', $msg );
    }
    
    function editFood()
    {
        JRequest::setVar('view', 'editFood');
        JRequest::setVar('hidemainmenu', 1);
        
        $model = $this->getModel('food');
        $user =& JFactory::getUser();
        
        $view =& $this->getView('editfood', 'html');
        $view->setModel($this->getModel('Food'), true);
        $view->setModel($this->getModel('FoodCategories'));
        $view->setModel($this->getModel('Variable'));
        $view->setModel($this->getModel('Foodvariable'));
        
        $view->display();
        
    }
    
    function saveFood() {
        $model = $this->getModel('food');
        $food_variable_model = $this->getModel('foodvariable');
        $user =& JFactory::getUser();
        
        // applying all changes
        $id = $model->store();
        $food_variable_model->apply($id);
    
        $link 	= 'index.php?option=com_joomdiet&view=food';
        $this->setRedirect($link, JText::_('COM_JOOMDIET_FOOD_SAVED'));
    }
    
    function addvariable() 
    {
        JRequest::setVar('view', 'editFood');
        JRequest::setVar('hidemainmenu', 1);
        
        $model = $this->getModel('food');
        $user =& JFactory::getUser();
        
        // saving or creating food data
        $id = $model->store();
        
        JRequest::setVar('cid', array($id));
        
        // saving or creating food variables data
        $variable_id = JRequest::getVar('variableselector');
        $food_variable_model = $this->getModel('foodvariable');
        $food_variable_model->insert($id, $variable_id);
        
        // loading edit view
        $view =& $this->getView('editfood', 'html');
        $view->setModel($this->getModel('Food'), true);
        $view->setModel($this->getModel('FoodCategories'));
        $view->setModel($this->getModel('Variable'));
        $view->setModel($this->getModel('Foodvariable'));
        
        $view->display();
    }
    
    function deletevariable()
    {
        JRequest::setVar('view', 'editFood');
        JRequest::setVar('hidemainmenu', 1);
        
        $model = $this->getModel('food');
        $food_variable_model = $this->getModel('foodvariable');
        $user =& JFactory::getUser();
        
        // delete selected variable
        $id = JRequest::getVar('fid');
        $vid = JRequest::getVar('vid');
        $food_variable_model->delete($id, $vid);
        
        // setting selected food
        JRequest::setVar('cid', array($id));
        
        // loading edit view
        $view =& $this->getView('editfood', 'html');
        $view->setModel($this->getModel('Food'), true);
        $view->setModel($this->getModel('FoodCategories'));
        $view->setModel($this->getModel('Variable'));
        $view->setModel($this->getModel('Foodvariable'));
        
        $view->display();
    }
    
    function applyfood()
    {
        JRequest::setVar('view', 'editFood');
        JRequest::setVar('hidemainmenu', 1);
        
        $model = $this->getModel('food');
        $food_variable_model = $this->getModel('foodvariable');
        $user =& JFactory::getUser();
        
        // applying all changes
        $id = $model->store();
        $food_variable_model->apply($id);
    
        // setting selected food
        JRequest::setVar('cid', array($id));
        
        // loading edit view
        $view =& $this->getView('editfood', 'html');
        $view->setModel($this->getModel('Food'), true);
        $view->setModel($this->getModel('FoodCategories'));
        $view->setModel($this->getModel('Variable'));
        $view->setModel($this->getModel('Foodvariable'));
        
        $view->display();
    }
}
?>
